import org.json.simple.parser.ParseException;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

public class MainForm {

    private JPanel jpanel1;
    private JTextField textField_no;
    private JTextField textField_name;
    private JTextField textField_sex;
    private JTextField textField_class;
    private JButton buttonLL;
    private JButton buttonL;
    private JButton buttonR;
    private JButton buttonRR;
    private JPanel jpanel2;
    private JButton button_save;
    private JButton button_del;
    private JButton button_clear;
    private JLabel lable_count;
    private JButton Button_refreash;
    static LinkedList<Student> students = new LinkedList<>();
    //存储信息的链表
    static int index = 0;

    public MainForm() {

        //刷新按钮
        Button_refreash.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                lable_count.setText("正在连接mongodb...");
                try {
                    students = MongoDB.GetStudentFromMongo();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                buttonLL.doClick();

            }
        });

        buttonLL.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //从链表中拿到第一个
                Student st1 = students.getFirst();
                //填充textbox
                setStudentInBox(st1);
                //错误弹窗
                testStudentstatus(st1);
                //设定序号显示
                index = 0;
                lable_count.setText(get序号_总数(st1.no));
            }
        });
        buttonL.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if(index == 0)return;
                else {
                    index -= 1;
                    Student st1 = students.get(index);
                    setStudentInBox(st1);
                    testStudentstatus(st1);
                    lable_count.setText(get序号_总数(st1.no));
                }
            }
        });

        buttonR.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if(index == students.size()-1)return;
                else {
                    index += 1;
                    Student st1 = students.get(index);
                    setStudentInBox(st1);
                    testStudentstatus(st1);
                    lable_count.setText(get序号_总数(st1.no));
                }
            }
        });
        buttonRR.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                index = students.size()-1;
                Student st1 = students.get(index);
                setStudentInBox(st1);
                testStudentstatus(st1);
                lable_count.setText(get序号_总数(st1.no));
            }
        });

        button_del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Student st = getStudent();
                // 首先查看数据库中有没有这个人
                boolean have = false;
                for (var item : students){
                    if (item.no.equals(st.no))have = true;
                }
                if(have) {
                    MongoDB.Delstudent(st.no);
                    JOptionPane.showMessageDialog(null,"学生: "+ st.name+"已成功删除");
                    buttonLL.doClick();
                }else
                    JOptionPane.showMessageDialog(null,"错误,学生不存在,请刷新");
            }
        });

        button_save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Student st = getStudent();//通过当前的textbox获取一个student
                if(testStudentstatus(st))//先判断textbox是否填写完整
                {
                    boolean isnewone = true;//是新生bool值
                    for (var item : students){
                        if (item.no.equals(st.no))isnewone = false; //通过是否是新的学号判断是否是新生
                    }
                    if(isnewone)MongoDB.InsertStudent(st);//是新生就插入
                    else MongoDB.Updatestudent(st);//是老生就更新

//                更新一下students列表
                    try {
                        students = MongoDB.GetStudentFromMongo();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    // 如果是新生,更新 序号
                    if (isnewone) lable_count.setText(get序号_总数(st.no));

                    JOptionPane.showMessageDialog(null,"是新生:"+isnewone+",保存成功!");
                }

            }
        });
        button_clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                textField_no.setText("");
                textField_name.setText("");
                textField_sex.setText("");
                textField_class.setText("");
            }
        });
    }

    /** get序号_总数 string
     * @param no 现在显示的no(学号)
     * @return
     */
    public static String get序号_总数(String no){
        int all = students.size();
        int flag = 0;
        for(var item : students)
        {
            flag ++;
            if(item.no.equals(no)) break; // 如果students中的学号等于要查的学号
        }
        return  Integer.toString(flag) +"/" +Integer.toString(all);
    }

    /**PUT STUDENTinfo in textfiled
     * @param st STUDENT
     */
    private  void setStudentInBox(Student st) {
        textField_no.setText(st.no);
        textField_name.setText(st.name);
        textField_sex.setText(st.sex);
        textField_class.setText(st.classs);
    }

    /** get info fromn textfiled
     * @return student
     */
    private Student getStudent(){
        Student st = new Student(
                textField_no.getText(),
                textField_name.getText(),
                textField_sex.getText(),
                textField_class.getText()
        );
        return st;
    }

    /**
     * return student status , false for bad , and show messagebox
     * @param st student to test
     * @return
     */
    private boolean testStudentstatus(Student st){
         if (st.no.equals(""))JOptionPane.showMessageDialog(null,"错误,学生无学号");
        else if (st.name.equals(""))JOptionPane.showMessageDialog(null,"错误,学生无名");
        else if (st.sex.equals(""))JOptionPane.showMessageDialog(null,"错误,学生无性别");
        else if (st.classs.equals(""))JOptionPane.showMessageDialog(null,"错误,学生无班级");
        else return true;
        return false;
    }

    public static void main(String[] args) throws ParseException {

        JFrame frame = new JFrame("MainForm");
        frame.setContentPane(new MainForm().jpanel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setTitle("学生基本信息");
        // window position set
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        frame.setLocation(width/2 - 100, height/2 - 100);

    }

}
